<template>
  <rx-dialog @handOk="handleSubmit" @cancel="cancel">
    <a-form :form="form">
      <a-form-item style="display:none">
        <a-input v-decorator="['groupId']"/>
      </a-form-item>
      <a-form-item
        :labelCol="labelCol"
        :wrapperCol="wrapperCol"
        label=""
      >
        <a-tree
          checkable
          v-model="checkedKeys"
          @check="onCheck"
          :treeData="permissions"
        >
        </a-tree>
      </a-form-item>
    </a-form>
  </rx-dialog>
</template>

<script>
  import OsGroupApi from "@/api/user/org/osGroup"
  import OsGroupApiApi from "@/api/user/org/osGroupApi"
  import PublicApi from "@/api/user/org/public"
  import { BaseForm } from "@lowcode/jpaas-common-lib"

  export default {
    name: 'OsGroupApiMenuEdit',
    mixins:[BaseForm],
    data () {
      return {
        permissions:[],
        treeCheck: true,
        id: null,
        allMenusList: [],
        checkedKeys: [],
        halfCheckedKeys: []
      }
    },
    created () {
      this.loadPermissions()
    },
    methods: {
      get(id){
        return OsGroupApi.getOsGroup(id);
      },
      onload_(values){
        if (values && values.groupId) {
          var self_=this;
          this.id = values.groupId;
          OsGroupApiApi.findListByGroupId(values.groupId).then(res => {
            // 因为antd 树插件勾选父节点会导致所有子节点选中,所以过滤所有父节点
            self_.checkedKeys = res.map(m => m.apiPath);
          })
        }
      },
      onCheck (checkedKeys, info) {
        this.checkedKeys = checkedKeys
        this.halfCheckedKeys = info.halfCheckedKeys
      },
      loadPermissions () {
        var self_=this;
        PublicApi.getAppServices().then(res=>{
          res.forEach(item=>{
            var app={title:item,key:item};
            self_.permissions.push(app);
            PublicApi.getAllClassByAppName(item).then(res=>{
              app.children=res;
              res.forEach(item=>{
                self_.allMenusList.push(...item.children);
              })
            })
          })
        })
      },
      validForm(values){
        const checkedAll = this.checkedKeys.concat(this.halfCheckedKeys);
        if (!checkedAll.length > 0) {
          this.$message.warning(this.$ti18('msa.selLeastAuthTips','请至少选择一个权限','osGroup'))
          return false;
        }
        return true;
      },
      save(values){
        const data = []
        var this_=this;
        const checkedAll = this.checkedKeys.concat(this.halfCheckedKeys);
        this.allMenusList.forEach(item => {
          item.children.forEach(menu =>{
            checkedAll.forEach(checkId => {
              if (menu.key === checkId) {
                var obj = {
                  groupId: this_.id,
                  apiMethod:menu.methodType,
                  apiPath:menu.key,
                  service:menu.service
                }
                data.push(obj)
              }
            })
          })
        })
        return OsGroupApiApi.saveOsGroupApi(Object.assign(data));
      }
    }
  }
</script>

<style scoped>

</style>
